#ifndef STRATEGY_USER_3_H_
#define STRATEGY_USER_3_H_
#pragma once
#include "strategy_sys.h"

typedef struct userdata_stra3
{
	userdata_stra3()
	{
		reqvol = 0;
		tradeprice = 0;
		direct = 0; //THOST_FTDC_D_Buy for up  THOST_FTDC_D_Sell for down 
		offset = 0;
		memset(inst, 0, sizeof(inst));
		
	}

	
	
	char offset; //#define THOST_FTDC_OF_Open '0' // #define THOST_FTDC_OF_CloseToday '3'

	char direct;//THOST_FTDC_D_Buy  '0'  //THOST_FTDC_D_Sell '1'

	int reqvol; 

	double tradeprice;

	char inst[8];

}userdata_stra3;



namespace StrategyUser3_CInstMap_NameSpace
{
	class CInstMap
	{
	public:

		//CInstMap(const char* pinst, int vol, double _pricetick, unsigned int _stopcnt_cl, BarSeries* _pbarser);

		CInstMap(char* inst, userdata_stra3* puserdata, double _pricetick);

		~CInstMap();


	public:

		int state;


		userdata_stra3 userdata; 


		double pricetick;

		TThostFtdcOrderRefType orderref;

	};
}





class CStrategyUser3: public CStrategySys
{
public:

	CStrategyUser3(string threadid);


	virtual ~CStrategyUser3(){};

private:

	virtual bool Init(BarInfo* pbar, map<string, vector<BarSeries*>>& mapuserbar, char* puserdata = NULL);

	virtual bool Destroy(BarInfo* pbar, map<string, vector<BarSeries*>>& mapuserbar);

	virtual bool Edit(BarInfo* pbar, char* puserdata);

	virtual void Run(BarData* ptickdata, char* inst);

	virtual void OnOrder(CThostFtdcOrderField *pOrder);

	virtual void OnTrade(CThostFtdcTradeField *pTrade);

	virtual void OnCancelOrder(CThostFtdcOrderField *pOrder);

	virtual void OnCancelOrderFailed(CThostFtdcInputOrderActionField *pCancelOrderFailed);

	virtual void OnStrategyCom(unsigned int size, char* pdata){};

private:

	///*void NotTimeout(CInstMap* pinstmap);

	//void Timingout(CInstMap* pinstmap);*/

	bool Open(BarData* ptickdata, StrategyUser3_CInstMap_NameSpace::CInstMap* pinstmap);

	//typedef enum CloseToday_Type
	//{
	//	notcancel = 1, cancelfailed, cancelsuccess
	//}CloseToday_Type;
	//bool CloseToday(CInstMap* pinstmap, CloseToday_Type closetype);

	//bool Cancel(CInstMap* pinstmap);

	//bool CancelAndClosetoday(char* prefix, CInstMap* pinstmap);




	void Print_SyncOrder(CThostFtdcOrderField *order);


	void InitTradetime();

	bool InTradeTime(SYSTEMTIME& curtime);

private:

	map<string , StrategyUser3_CInstMap_NameSpace::CInstMap* > inst_map;

	SYSTEMTIME tradetime[2];

};





#endif